﻿using System;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Freedom
{
	/// <summary>
	/// 基于串口的自由协议，需要在地址里传入报文信息，也可以传入数据偏移信息，<see cref="P:HslCommunication.Serial.SerialDeviceBase.ByteTransform" />默认为<see cref="T:HslCommunication.Core.RegularByteTransform" />
	/// </summary>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\FreedomExample.cs" region="Sample5" title="实例化" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\FreedomExample.cs" region="Sample6" title="读取" />
	/// </example>
	// Token: 0x020000A8 RID: 168
	public class FreedomSerial : SerialDeviceBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000C85 RID: 3205 RVA: 0x0004A35A File Offset: 0x0004855A
		public FreedomSerial()
		{
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x06000C86 RID: 3206 RVA: 0x0004A370 File Offset: 0x00048570
		[HslMqttApi("ReadByteArray", "特殊的地址格式，需要采用解析包起始地址的报文，例如 modbus 协议为 stx=9;00 00 00 00 00 06 01 03 00 64 00 01")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[], int> operateResult = FreedomTcpNet.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content1);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult.Content2 >= operateResult2.Content.Length;
					if (flag3)
					{
						result = new OperateResult<byte[]>(StringResources.Language.ReceiveDataLengthTooShort);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content.RemoveBegin(operateResult.Content2));
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000C87 RID: 3207 RVA: 0x0004A400 File Offset: 0x00048600
		public override OperateResult Write(string address, byte[] value)
		{
			return this.Read(address, 0);
		}

		/// <inheritdoc />
		// Token: 0x06000C88 RID: 3208 RVA: 0x0004A41A File Offset: 0x0004861A
		public override string ToString()
		{
			return string.Format("FreedomSerial<{0}>[{1}:{2}]", base.ByteTransform.GetType(), base.PortName, base.BaudRate);
		}
	}
}
